package vn.com.demand.lux.preferences;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import vn.com.demand.lux.Activator;

import com.swtdesigner.ResourceManager;

public class CriteriaPreference extends PreferencePage implements
		IWorkbenchPreferencePage {

	private Text installmentAmount;
	private Text interest;
	private Text payment;
	private Text vatAmount;
	private Text bookingFee;
	private Text revenue;
	private Text accountJournalCode;
	private Text accountDivision;
	private Text finalcial;
	private Text deferred;
	private Text division;
	private Text installmentLedger;
	private Text journalCode;
	private Button interestChooseDefaultValueButton;
	private Button accountChooseDefaultValueButton;
	private Button accountChooseLastUseButton;
	private Button interestChooseLastUseButton;

	/**
	 * Create the preference page
	 */
	public CriteriaPreference() {
		super();
		setTitle("Database Connection");
		setImageDescriptor(ResourceManager.getPluginImageDescriptor(Activator
				.getDefault(), "icons/actions/database.png"));
	}

	/**
	 * Create contents of the preference page
	 * 
	 * @param parent
	 */
	@Override
	public Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new FormLayout());
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		container.setLayoutData(data);

		final Group databaseGroup = new Group(container, SWT.NONE);
		databaseGroup.setText("Interest Forward");
		final FormData fd_databaseGroup = new FormData();
		fd_databaseGroup.right = new FormAttachment(100, -9);
		fd_databaseGroup.left = new FormAttachment(0, 0);
		fd_databaseGroup.top = new FormAttachment(0, 5);
		databaseGroup.setLayoutData(fd_databaseGroup);

		final Label sqlServerLabel = new Label(databaseGroup, SWT.RIGHT);
		sqlServerLabel.setBounds(15, 50, 80, 20);
		sqlServerLabel.setText("Division:");
		String[] properties = Activator.getDefault().getDatabaseProperties();
		if (null == properties || properties.length <= 0) {
			properties = Activator.getDefault().getDefaultDatabaseProperties();
		}

		Label txtPort;
		txtPort = new Label(databaseGroup, SWT.RIGHT);
		txtPort.setBounds(15, 75, 80, 20);
		txtPort.setText("Journal Code:");

		journalCode = new Text(databaseGroup, SWT.BORDER);
		journalCode.setBounds(110, 75, 90, 19);
		journalCode.setText(properties[1]);

		Group sqlServerAuthenticationGroup;
		sqlServerAuthenticationGroup = new Group(container, SWT.NONE);
		fd_databaseGroup.bottom = new FormAttachment(sqlServerAuthenticationGroup, -5, SWT.TOP);
		sqlServerAuthenticationGroup.setText("Account Receivable");
		final FormData fd_sqlServerAuthenticationGroup = new FormData();
		fd_sqlServerAuthenticationGroup.bottom = new FormAttachment(100, -25);
		fd_sqlServerAuthenticationGroup.top = new FormAttachment(0, 160);
		fd_sqlServerAuthenticationGroup.right = new FormAttachment(100, -9);
		fd_sqlServerAuthenticationGroup.left = new FormAttachment(0, 5);
		sqlServerAuthenticationGroup
				.setLayoutData(fd_sqlServerAuthenticationGroup);

		final Label sqlServerLabel_2 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		sqlServerLabel_2.setBounds(10, 50, 80, 20);
		sqlServerLabel_2.setText("Division:");

		accountDivision = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		accountDivision.setBounds(105, 50, 90, 19);
		accountDivision.setText(properties[5]);

		final Label sqlServerLabel_1_1 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		sqlServerLabel_1_1.setBounds(236, 50, 107, 20);
		sqlServerLabel_1_1.setText("Journal Code:");

		accountJournalCode = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		accountJournalCode.setBounds(358, 50, 90, 19);
		accountJournalCode.setText(properties[6]);

		final Label sqlServerLabel_2_1 = new Label(sqlServerAuthenticationGroup, SWT.NONE);
		sqlServerLabel_2_1.setBounds(10, 75, 90, 20);
		sqlServerLabel_2_1.setText("Ledger information:");

		final Label sqlServerLabel_3 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		sqlServerLabel_3.setBounds(10, 100, 80, 20);
		sqlServerLabel_3.setText("Revenue:");

		final Label sqlServerLabel_1_2 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		sqlServerLabel_1_2.setBounds(233, 100, 110, 20);
		sqlServerLabel_1_2.setText("Booking fee:");

		revenue = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		revenue.setBounds(105, 100, 90, 19);
		revenue.setText(properties[7]);

		bookingFee = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		bookingFee.setBounds(358, 100, 90, 19);
		bookingFee.setText(properties[8]);

		final Label txtPort_2 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		txtPort_2.setBounds(10, 125, 80, 20);
		txtPort_2.setText("VAT Amount:");

		vatAmount = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		vatAmount.setBounds(105, 125, 90, 19);
		vatAmount.setText(properties[9]);

		final Label txtPort_1_1 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		txtPort_1_1.setBounds(231, 125, 112, 20);
		txtPort_1_1.setText("Down payment:");

		payment = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		payment.setBounds(358, 125, 90, 19);
		payment.setText(properties[10]);

		final Label databaseNameLabel_1 = new Label(sqlServerAuthenticationGroup, SWT.NONE);
		databaseNameLabel_1.setBounds(10, 150, 80, 20);
		databaseNameLabel_1.setAlignment(SWT.RIGHT);
		databaseNameLabel_1.setText("Interest:");

		interest = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		interest.setBounds(105, 150, 90, 20);
		interest.setText(properties[11]);

		final Label txtPort_1_1_1 = new Label(sqlServerAuthenticationGroup, SWT.RIGHT);
		txtPort_1_1_1.setBounds(239, 151, 104, 20);
		txtPort_1_1_1.setText("Installment Amount:");

		installmentAmount = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		installmentAmount.setBounds(358, 151, 90, 19);
		installmentAmount.setText(properties[12]);

		accountChooseDefaultValueButton = new Button(sqlServerAuthenticationGroup, SWT.RADIO);
		accountChooseDefaultValueButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				boolean isSelected = accountChooseDefaultValueButton.getSelection();
				if(isSelected) {
					setEnableAccountTextbox(true);
				} 
			}
		});
		accountChooseDefaultValueButton.setText("Choose default value:");
		accountChooseDefaultValueButton.setBounds(10, 20, 128, 19);

		accountChooseLastUseButton = new Button(sqlServerAuthenticationGroup, SWT.RADIO);
		accountChooseLastUseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				boolean isSelected = accountChooseLastUseButton.getSelection();
				if(isSelected) {
					setEnableAccountTextbox(false);
				} 
			}
		});
		if("true".equals(properties[14])) {
			accountChooseDefaultValueButton.setSelection(true);
			accountChooseLastUseButton.setSelection(false);
			setEnableAccountTextbox(true);
		} else {
			accountChooseDefaultValueButton.setSelection(false);
			accountChooseLastUseButton.setSelection(true);
			setEnableAccountTextbox(false);
		}
		
		accountChooseLastUseButton.setText("Choose last use:");
		accountChooseLastUseButton.setBounds(10, 176, 112, 16);

		Label databaseNameLabel;
		databaseNameLabel = new Label(databaseGroup, SWT.NONE);
		databaseNameLabel.setBounds(5, 100, 90, 20);
		databaseNameLabel.setAlignment(SWT.RIGHT);
		databaseNameLabel.setText("Installment ledger:");

		installmentLedger = new Text(databaseGroup, SWT.BORDER);
		installmentLedger.setText(properties[2]);
		installmentLedger.setBounds(110, 100, 90, 20);

		division = new Text(databaseGroup, SWT.BORDER);
		division.setText(properties[0]);
		division.setBounds(110, 50, 90, 19);

		final Label sqlServerLabel_1 = new Label(databaseGroup, SWT.RIGHT);
		sqlServerLabel_1.setBounds(221, 50, 127, 20);
		sqlServerLabel_1.setText("Deferred revenue ledger:");

		deferred = new Text(databaseGroup, SWT.BORDER);
		deferred.setText(properties[3]);
		deferred.setBounds(363, 50, 90, 19);

		final Label txtPort_1 = new Label(databaseGroup, SWT.RIGHT);
		txtPort_1.setBounds(221, 75, 127, 20);
		txtPort_1.setText("Financial income ledger:");

		finalcial = new Text(databaseGroup, SWT.BORDER);
		finalcial.setText(properties[4]);
		finalcial.setBounds(363, 75, 90, 19);

		interestChooseDefaultValueButton = new Button(databaseGroup, SWT.RADIO);
		interestChooseDefaultValueButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				boolean isSelected = interestChooseDefaultValueButton.getSelection();
				if(isSelected) {
					setEnableInterestTextbox(true);
				} 
			}
		});
		interestChooseDefaultValueButton.setText("Choose default value:");
		interestChooseDefaultValueButton.setBounds(9, 23, 155, 16);

		interestChooseLastUseButton = new Button(databaseGroup, SWT.RADIO);
		interestChooseLastUseButton.setText("Choose last use:");
		interestChooseLastUseButton.setBounds(10, 125, 127, 16);
		interestChooseLastUseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				boolean isSelected = interestChooseLastUseButton.getSelection();
				if(isSelected) {
					setEnableInterestTextbox(false);
				} 
			}
		});
		if("true".equals(properties[13])) {
			interestChooseDefaultValueButton.setSelection(true);
			interestChooseLastUseButton.setSelection(false);
			setEnableInterestTextbox(true);
		} else {
			interestChooseDefaultValueButton.setSelection(false);
			interestChooseLastUseButton.setSelection(true);
			setEnableInterestTextbox(false);
		}
		//
		return container;
	}

	/**
	 * Initialize the preference page
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		division.setText(PreferenceConstants.INTEREST_SUMMARY_DIVISION);
		journalCode.setText(PreferenceConstants.INTEREST_SUMMARY_JOURNAL_CODE);
		installmentLedger.setText(PreferenceConstants.INTEREST_SUMMARY_INSTALLMENT_LEDGER);
		deferred.setText(PreferenceConstants.INTEREST_SUMMARY_DEFERRED_REVENUE_LEDGER);
		finalcial.setText(PreferenceConstants.INTEREST_SUMMARY_FINANCIAL_INCOME_LEDGER);
		accountDivision.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_DIVISION);
		accountJournalCode.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_JOURNAL_CODE);
		revenue.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_REVENUE);
		bookingFee.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_BOOKING_FEE);
		vatAmount.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_VAT_AMOUNT);
		payment.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_DOWNPAYMENT);
		interest.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_INTEREST);
		installmentAmount.setText(PreferenceConstants.ACCOUNT_RECEIVABLE_INSTALLMENT_AMOUNT);
		interestChooseDefaultValueButton.setSelection(true);
		accountChooseDefaultValueButton.setSelection(true);
		interestChooseLastUseButton.setSelection(false);
		accountChooseLastUseButton.setSelection(false);
		setEnableInterestTextbox(true);
		setEnableAccountTextbox(true);
	}

	@Override
	public boolean performOk() {
		String[] elements = new String[15];
		elements[0] = division.getText();
		elements[1] = journalCode.getText();
		elements[2] = installmentLedger.getText();
		elements[3] = deferred.getText();
		elements[4] = finalcial.getText();
		elements[5] = accountDivision.getText();
		elements[6] = accountJournalCode.getText();
		elements[7] = revenue.getText();
		elements[8] = bookingFee.getText();
		elements[9] = vatAmount.getText();
		elements[10] = payment.getText();
		elements[11] = interest.getText();
		elements[12]= installmentAmount.getText();
		elements[13]= "" + interestChooseDefaultValueButton.getSelection();
		elements[14]= "" + accountChooseDefaultValueButton.getSelection();
		
		Activator.getDefault().setDatabasePropertiesPreferences(elements);
		return super.performOk();
	}

	@Override
	protected void contributeButtons(Composite parent) {
		super.contributeButtons(parent);
	}

	@Override
	protected void performApply() {
		super.performApply();
		String[] elements = new String[15];
		elements[0] = division.getText();
		elements[1] = journalCode.getText();
		elements[2] = installmentLedger.getText();
		elements[3] = deferred.getText();
		elements[4] = finalcial.getText();
		elements[5] = accountDivision.getText();
		elements[6] = accountJournalCode.getText();
		elements[7] = revenue.getText();
		elements[8] = bookingFee.getText();
		elements[9] = vatAmount.getText();
		elements[10] = payment.getText();
		elements[11] = interest.getText();
		elements[12]= installmentAmount.getText();
		elements[13]= "" + interestChooseDefaultValueButton.getSelection();
		elements[14]= "" + accountChooseDefaultValueButton.getSelection();
		
		Activator.getDefault().setDatabasePropertiesPreferences(elements);
	}
	
	private void setEnableInterestTextbox(boolean b) {
		division.setEnabled(b);
		journalCode.setEnabled(b);
		installmentLedger.setEnabled(b);
		deferred.setEnabled(b);
		finalcial.setEnabled(b);
	}
	
	private void setEnableAccountTextbox(boolean b) {
		accountDivision.setEnabled(b);
		accountJournalCode.setEnabled(b);
		revenue.setEnabled(b);
		bookingFee.setEnabled(b);
		vatAmount.setEnabled(b);
		payment.setEnabled(b);
		interest.setEnabled(b);
		installmentAmount.setEnabled(b);
	}
}
